import React, { Component } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import { Link } from 'react-router'
import { Table, Spin, Form, Row, Col, Input, Button } from 'antd'

import Ajax from 'lib/Ajax'

class ModuleName extends Component {
	constructor(props) {
    super(props);
    this.state = {
      loading: true,
      data: [],
      openKeys: []
    }
  }

  componentDidMount(){
  	this.loadData();
  }

  loadData = () => {
    var self = this;
    var openKeys = [];
    Ajax({
      url: '/authority/list',
      success: function(data){
        const loop = (data) => {
          data.forEach((item) => {
            if(item.children&&item.children.length > 0){
              openKeys.push(item.id);
              loop(item.children);
            }else{
              delete item.children
            }
          });
        };
        loop(data);
        self.setState({
          loading: false,
          data,
          openKeys
        });
      },
      error: function(error){
        self.setState({
          loading: false
        });
        message.error(error);
      }
    });
  }
  
  render() {
    const columns = [{
      title: 'id',
      dataIndex: 'id',
      key: 'id'
    }, {
      title: '权限名',
      dataIndex: 'name',
      key: 'name'
    }, {
      title: '创建者',
      dataIndex: 'creator',
      key: 'creator'
    }];
    console.log(this.state.openKeys);
    return (
    	<div>
        <Table 
          columns={columns}
          dataSource={this.state.data}
          bordered
          pagination={false}
          rowKey="id"
          loading={ this.state.loading }
          expandedRowKeys={this.state.openKeys}
          onExpandedRowsChange={ (openKeys)=>{ this.setState({ openKeys }) }}
        />
    	</div>
    )
  }
}

function mapStateToProps(state) {
  return {
    
  }
}

function mapDispatchToProps(dispatch) {
  return bindActionCreators({  }, dispatch)
}

module.exports = connect(mapStateToProps,mapDispatchToProps)(ModuleName)
